package com.catsic.pub.ftp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.base.utils.FtpUtils;
import com.catsic.pub.condition.FtpCondition;
import com.catsic.pub.entity.Ftp;
import com.catsic.pub.exception.CatsicExceptionCode;
import com.catsic.pub.ftp.dao.FtpDao;
import com.catsic.pub.ftp.service.FtpService;
import com.catsic.pub.specification.FtpSpec;
@Service("ftpService")
public class FtpServiceImpl extends BaseServiceImpl<Ftp, String> implements FtpService {

	@Autowired
	private FtpDao ftpDao;
	
/*	@Override
	protected BaseDao<Ftp, String> getEntityDao() {
		return ftpDao;
	}*/
	@Override
	public Ftp add(Ftp ftp) {
		super.add(ftp);
		return ftp;
	}

	@Override
	public void update(Ftp ftp) {
		super.update(ftp);
	}

	@Override
	public void delete(String id) {
		super.delete(id);
	}
	
	@Override
	public void test(String id) {
		Ftp ftp = findById(id);
		FtpUtils fu = new FtpUtils(ftp.getFtpip(),ftp.getFtpport(),ftp.getFtpuser(),ftp.getFtppassword());
		fu.login();
		fu.exit();
	}
	
	@Override
	public Ftp findById(String id) {
		Ftp ftp = super.getById(id);
		if(ftp == null){
			throw new CatsicException(CatsicExceptionCode.FTP管理_不存在的FTP服务器);
		}
		return ftp;
	}
	
	@Override
	public Page<Ftp> findAll(FtpCondition condition, Pageable pageable) {
		FtpSpec spec = new FtpSpec(condition);
		Page<Ftp> page = ftpDao.findAll(spec,pageable);
		List<Ftp> listForm = new ArrayList<Ftp>();
		for(Ftp ftp : page.getContent()){
			listForm.add(ftp);
		}
		Page<Ftp> page_ = new PageImpl<Ftp>(listForm,pageable,page.getTotalElements());
		return page_;
	}
	
	@Override
	public Ftp findByOrganCode(String organCode) {
		return ftpDao.findByOrganCode(organCode);
	}
}
